import {DeviceConfig, DeviceType} from "./DeviceConfig";
// import MqttClient from "./MqttClient";
import {OpsAuth, TopicFactory, TopicFunction} from "./TopicFactory";
import EventNotifyPayload from "./payload/EventNotifyPayload";
import PropertyRequestPayload from "./payload/PropertyRequestPayload";
import WebSocketClient from "./WebSocketClient";


export default class MqttDevice {

  constructor(deviceBasicData, deviceModel) {

    this.topicFactory = new TopicFactory();
    this.topicFactory.init();

    this.deviceConfig = new DeviceConfig(DeviceType.CLIENT, deviceBasicData, deviceModel);
    this.deviceBasicData = deviceBasicData;
    this.connect = new WebSocketClient();
    this.connect.username = deviceBasicData.username;
    this.connect.password = deviceBasicData.password;
    this.connect.clientId = deviceBasicData.clientId;

    // console.log("MqttDevice", deviceBasicData, this.connect);

    this.connect.addr = deviceBasicData.addr;

  }

  connectServer = () =>{
    this.connect.connect();
  }


  close() {
    this.connect.close();
  }

  ping = () =>{
    this.connect.ping();
  }

  subscribe = (topics) => {
    this.connect.subscribe(topics);
  }

  send(request) {
    return this.connect.send(request, null);
  }

  setDataListener(dataListener) {
    this.dataListener = dataListener;
    this.connect.setDataListener(dataListener);
  }

  subscribe(topics) {
    let res = this.connect.subscribe(topics);
    return res
  }

  pushPropertyValue = (params) => {
    let map = {
      "${productKey}": this.deviceBasicData.getProductKey(),
      "${deviceName}": this.deviceBasicData.getDeviceName()
    };
    let topic = this.topicFactory.getTopic(TopicFunction.PropertyPost, OpsAuth.PUSH, map);

    let payload = new PropertyRequestPayload(this.deviceBasicData.getProductKey(), this.deviceBasicData.getDeviceName());
    payload.params = params;

    let request = {
      topic: topic,
      mWrapperRequest: payload
    };

    let flag = this.send(request);

    return flag;
  }

  pushEvent(identifier, params) {
    let map = {
      "${productKey}": this.deviceBasicData.getProductKey(),
      "${deviceName}": this.deviceBasicData.getDeviceName()
    };
    let topic = this.topicFactory.getTopic(TopicFunction.EventPost, OpsAuth.PUSH, map);

    let payload = new EventNotifyPayload(this.deviceBasicData.getProductKey(), this.deviceBasicData.getDeviceName());
    payload.method = (identifier);
    payload.params = params;

    let request = {
      topic: topic,
      id: payload.id,
      mWrapperRequest: payload
    };

    let flag = this.send(request, null);

    return flag;
  }

  resSetPropertyValue = (payload) =>{
    let map = {
      "${productKey}": this.deviceBasicData.getProductKey(),
      "${deviceName}": this.deviceBasicData.getDeviceName()
    };
    let topic = topicFactory.getTopic(TopicFunction.PropertySet, OpsAuth.PUSH, map);

    let response = {
      topic: topic,
      mResponse: payload
    };

    let flag = this.send(response);
    return flag;
  }

  resService = (topic, payload) => {
    let response = {
      topic: topic,
      mResponse: payload
    };

    let flag = this.send(response);

    return flag;
  }


}
